<?php

namespace App\Models;

class AnnounceAg extends BaseModel
{
    protected $table = "announce_ag";
    const PREFIX = 'ag_';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where=[], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }


    //发布代理商公告
    public function getAnnounceList($request)
    {
        $keyword = $request['keyword'] ? $request['keyword'] : '';
        $where = [];
        $where['bid'] = session('adminId');
        if ($keyword) {
            $where['title'] = ['like','%'.$keyword.'%'];
        }
        $order['status'] = 'desc';
        $order['id'] = 'desc';
        $rows = $this->getList($where, '*', $order, 10);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'],request('page',1),10);
        }
        return $rows;
    }

    public function addAnnounce($request)
    {
        $data['title'] = $request['title'];
        $data['content'] = $request['content'];
        $data['type'] = $request['type'];
        $data['bid'] = session('adminId');
        $data['status'] = $request['status'] ? 1 : 0;
        $data['created_at'] = date('Y-m-d H:i:s',time());
        $data['updated_at'] = date('Y-m-d H:i:s', time());
        $res = $this->add(trim_fileds($data));
        $ajaxData['status'] = $res ? 1 : 0;
        $ajaxData['msg'] = '添加成功！';
        if (!$res) {
            $ajaxData['msg'] = '添加失败！';
        }
        return $ajaxData;
    }

    public function editAnnounce($request)
    {
        $where['id'] = $request['id'];
        $data['title'] = $request['title'];
        $data['content'] = $request['content'];
        $res = $this->up($where,$data);
        $ajaxData['status'] = $res ? 1 : 0;
        $ajaxData['msg'] = '修改成功！';
        if (!$res) {
            $ajaxData['msg'] = '修改失败！';
        }
        return $ajaxData;
    }

    public function changeStatus($request)
    {
        $where['type'] = $request['type'];
        $this->up($where,['status'=>0]);
        $where['id'] = $request['id'];
        $data['status'] = 1;
        $res = $this->up($where,$data);
        $ajaxData['status'] = $res ? 1 : 0;
        $ajaxData['msg'] = '发布成功！';
        if (!$res) {
            $ajaxData['msg'] = '发布失败！';
        }
        return $ajaxData;
    }

    public function getAgentAnnounceList()
    {
        $where['bid'] = session('agent')['aid'];
        $rows = $this->getList($where);
        if ($rows) {
            $rows = $rows->toArray();
            foreach ($rows as $k=>$v) {
                $rows[$k]['type'] = 2;
            }
        }
        return $rows;
    }

    public function getAgentAnnounceInfo($id)
    {
        $where['id'] = $id;
        return $this->getOne($where);
    }


    //删除公告
    public function delAnnounceDelById($request)
    {
        $where['id'] = $request['id'];
        $did = $this->del($where);
        if(!$did){
            return ['status'=>0,'msg'=>'删除失败！请重试'];
        }

        return ['status'=>1,'msg'=>'删除公告成功！'];

    }

}